package com.mxpio.erp.technology.controller;

import com.mxpio.erp.common.technology.entity.ItemQualityProp;
import com.mxpio.erp.common.technology.entity.ItemPropKey;
import com.mxpio.erp.common.technology.service.ItemQualityPropService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ItemQualityPropController", description = "物料质量属性")
@RestController
@RequestMapping("/erp/common/item/prop/quality/")
public class ItemQualityPropController {

	@Autowired
	private ItemQualityPropService itemQualityPropSerivce;

	@GetMapping("list")
	@Operation(summary = "物料质量属性列表", description = "获取物料质量属性列表", method = "GET")
	public Result<List<ItemQualityProp>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<ItemQualityProp> itemQualityProps = itemQualityPropSerivce.list(ItemQualityProp.class, criteria);
		return Result.OK(itemQualityProps);
	}

	@GetMapping("page")
	@Operation(summary = "物料质量属性列表", description = "获取物料质量属性列表", method = "GET")
	public Result<Page<ItemQualityProp>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo-1, pageSize);
		Page<ItemQualityProp> itemQualityProps = itemQualityPropSerivce.listPage(ItemQualityProp.class, page, criteria);
		return Result.OK(itemQualityProps);
	}

	@GetMapping("list/{propType}/{code}")
	@Operation(summary = "根据type，code获取物料质量属性", description = "根据type，code获取物料质量属性", method = "GET")
	public Result<ItemQualityProp> getById(@PathVariable(name = "propType", required = true) String propType,
			@PathVariable(name = "code", required = true) String code) {
		ItemPropKey key = new ItemPropKey();
		key.setPropType(propType);
		key.setCode(code);
		ItemQualityProp itemQualityProp = itemQualityPropSerivce.getById(ItemQualityProp.class, key);
		return Result.OK(itemQualityProp);
	}

	@PostMapping("add")
	@Operation(summary = "新增物料质量属性", description = "新增物料质量属性", method = "POST")
	public Result<ItemQualityProp> add(@RequestBody ItemQualityProp itemQualityProp) {
		itemQualityPropSerivce.save(itemQualityProp);
		return Result.OK(itemQualityProp);
	}

	@PutMapping("edit")
	@Operation(summary = "编辑物料质量属性", description = "编辑物料质量属性（全量）", method = "PUT")
	public Result<ItemQualityProp> edit(@RequestBody ItemQualityProp itemQualityProp) {
		itemQualityPropSerivce.update(itemQualityProp);
		return Result.OK(itemQualityProp);
	}

	@DeleteMapping("remove/{propType}/{code}")
	@Operation(summary = "删除物料质量属性", description = "删除物料质量属性", method = "DELETE")
	public Result<ItemQualityProp> remove(@PathVariable(name = "propType", required = true) String propType,
			@PathVariable(name = "code", required = true) String code) {
		ItemPropKey key = new ItemPropKey();
		key.setPropType(propType);
		key.setCode(code);
		itemQualityPropSerivce.delete(ItemQualityProp.class, key);
		return Result.OK();
	}

}
